package com.dk.service.impl;

import com.dk.mapper.TabRouteImgMapper;
import com.dk.mapper.TabRouteMapper;
import com.dk.pojo.TabRoute;
import com.dk.pojo.TabRouteImg;
import com.dk.pojo.TabRouteImgExample;
import com.dk.pojo.TabRouteVO;
import com.dk.service.TravelRouteService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class TravelRouteServiceImpl implements TravelRouteService {
    String key = "RouteForSet:";
    @Autowired
    TabRouteMapper tabRouteMapper;
    @Autowired
    TabRouteImgMapper tabRouteImgMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public PageInfo<TabRouteVO> getInfo(TabRouteVO tabRoute, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TabRouteVO> routes = tabRouteMapper.getInfo(tabRoute);
        PageInfo<TabRouteVO> pageInfo = new PageInfo<>(routes);
        return pageInfo;
    }

    @Override
    public int insertInfo(String[] bigPic, TabRoute tabRoute) {
        //route添加第一张图片
        String[] insert = insert(bigPic, tabRoute.getRimage());
        //去除空值
        String[] picContent = deleteArrayNull(insert);
        //添加route
        int i = tabRouteMapper.insertSelective(tabRoute);
        Integer rid = tabRoute.getRid();
        if (i>0){
            redisTemplate.delete(key);
        }
        System.out.println(Arrays.toString(picContent));
        for (String s : picContent) {
            TabRouteImg tabRouteImg = new TabRouteImg();
            tabRouteImg.setBigpic(s);
            tabRouteImg.setSmallpic(s);
            tabRouteImg.setRid(rid);
            tabRouteImgMapper.insertSelective(tabRouteImg);
        }

        return i;
    }

    private static String[] insert(String[] strings, String string) {
        if (strings == null) {
            strings = new String[0];
        }
        if (string.isEmpty()) {
            System.out.println("Empty string.");
            return null;
        }
        String[] resultString = new String[strings.length + 1];
        for (int i = 0; i < strings.length; i++) {
            resultString[i] = strings[i];
        }
        resultString[strings.length] = string;
        return resultString;
    }

    /**
     * String去除空字符或者null
     * @param string
     * @return
     */

    private String[] deleteArrayNull(String string[]) {
        String strArr[] = string;
        // step1: 定义⼀个list列表，并循环赋值
        ArrayList<String> strList = new ArrayList<String>();
        for (int i = 0; i < strArr.length; i++) {
            strList.add(strArr[i]);
        }
        // step2: 删除list列表中所有的空值
        while (strList.remove(null)) ;
        while (strList.remove("")) ;
        // step3: 把list列表转换给⼀个新定义的中间数组，并赋值给它
        String strArrLast[] = strList.toArray(new String[strList.size()]);
        return strArrLast;
    }


    @Override
    public int updateRoute(String[] bigPic, TabRoute tabRoute) {
        //route添加第一张图片
        String[] insert = insert(bigPic, tabRoute.getRimage());
        //去除空值
        String[] picContent = deleteArrayNull(insert);
        int i = tabRouteMapper.updateByPrimaryKeySelective(tabRoute);
        //清除redis
        Integer rid = tabRoute.getRid();
        if (i>0){
            redisTemplate.delete(key);
        }
        System.out.println(Arrays.toString(picContent));
        //先删除路线图片表对应rid原有的数据
        TabRouteImgExample example = new TabRouteImgExample();
        TabRouteImgExample.Criteria criteria = example.createCriteria();
        criteria.andRidEqualTo(rid);
        tabRouteImgMapper.deleteByExample(example);
        for (String s : picContent) {
            TabRouteImg tabRouteImg = new TabRouteImg();
            tabRouteImg.setBigpic(s);
            tabRouteImg.setSmallpic(s);
            tabRouteImg.setRid(rid);
            tabRouteImgMapper.insertSelective(tabRouteImg);
        }
        return i;
    }

    @Override
    public TabRoute getInfoById(Integer id) {
        return tabRouteMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteRouteById(Integer id) {
        int i = tabRouteMapper.deleteByPrimaryKey(id);
        if (i>0){
            redisTemplate.delete(key);
        }
        return i;
    }

    @Override
    public PageInfo<TabRouteVO> getInfoNotReviewed(TabRouteVO tabRoute, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<TabRouteVO> routes = tabRouteMapper.getInfoNotReviewed(tabRoute);
        PageInfo<TabRouteVO> pageInfo = new PageInfo<>(routes);
        return pageInfo;
    }

    @Override
    public int updateToPass(Integer id) {
        int i = tabRouteMapper.updateToPass(id);
        return i;
    }

    @Override
    public int updateToTurnDown(Integer id) {
        int i = tabRouteMapper.updateToTurnDown(id);
        return i;
    }

    @Override
    public int updateReUpload(Integer id) {
        int i = tabRouteMapper.updateReUpload(id);
        return i;
    }
}
